#include "ctamain.h"

ctaMain::ctaMain(int argc, char**argv) : QApplication(argc, argv)
{
    this->argc = argc;
    this->argv = argv;
}

void ctaMain::ctaInit()
{
    // Init Dialogs
    MainMenu = new MainMenuImpl;
    OptionsMenu = new OptionsMenuImpl();

    ctaSetup();
}

void ctaMain::ctaSetup()
{

    connect( MainMenu->exitButton, SIGNAL(clicked()), this, SLOT(quit()) );
    connect( MainMenu->optionsButton, SIGNAL(clicked()), this, SLOT(readConfig()) );
    connect( MainMenu->startSPGameButton, SIGNAL(clicked()), this, SLOT(startSPGame()) );
    connect( MainMenu->startMPGameButton, SIGNAL(clicked()), this, SLOT(startMPGame()) );
    connect( MainMenu->startNGameButton, SIGNAL(clicked()), this, SLOT(showServerExplorer()) );

}

void ctaMain::ctaRun()
{
    MainMenu->show();
    // Show MainMenu

}

// SLOTS //
void ctaMain::readConfig()
{
    ctaConfig_.readConfig();
    this->OptionsMenu->screenWidthLineEdit->setText(QString::number(ctaConfig_.XResolution));
    this->OptionsMenu->screenHeightLineEdit->setText(QString::number(ctaConfig_.YResolution));
    if(ctaConfig_.FullScreen == true)
    {
        this->OptionsMenu->fullScreenCheckBox->setCheckState(Qt::Checked);
    } else {
        this->OptionsMenu->fullScreenCheckBox->setCheckState(Qt::Unchecked);
    }
    this->connect(OptionsMenu->buttonBox, SIGNAL(accepted()), this, SLOT(saveConfig()) );
    this->connect(OptionsMenu->buttonBox, SIGNAL(rejected()), OptionsMenu, SLOT(close()) );
    this->OptionsMenu->exec();
}

void ctaMain::saveConfig()
{
    ctaConfig_.XResolution = OptionsMenu->screenWidthLineEdit->text().toInt();
    ctaConfig_.YResolution = OptionsMenu->screenHeightLineEdit->text().toInt();
    if(OptionsMenu->fullScreenCheckBox->checkState()==Qt::Checked)
    {
        ctaConfig_.FullScreen = int(true);
    } else {
        ctaConfig_.FullScreen = int(false);
    }
    ctaConfig_.saveConfig();
    OptionsMenu->close();
}

void ctaMain::startSPGame()
{

    ctaConfig_.readConfig();
    ctaConfig_.saveCSConfig();
    Game = new catchthealien();
    Game->setGameType("single");
    Game->Main(argc, argv);

}

void ctaMain::startMPGame()
{
    ctaConfig_.readConfig();
    ctaConfig_.saveCSConfig();
    Game = new catchthealien();
    Game->setGameType("multi");
    Game->Main(argc, argv);
}

void ctaMain::showServerExplorer()
{
    ServerExplorer = new ServerExplorerImpl();
    ServerExplorer->exec();
}

void ctaMain::startNGame()
{
}
